﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.PhanQuyen
{
    public partial class frmThemquyen : Form
    {
        public delegate void AddCTQ(Entities.ChiTietQuyen ctq);
        public AddCTQ ThemCTQ;

        public frmThemquyen()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        Entities.Quyen[] aq;
        public Entities.ChiTietQuyen[] actq;
        public string tennhomquyen;
        private void frmThemquyen_Load(object sender, EventArgs e)
        {

            DataTable dt = Common.StaticVariable.InterfaceObject.SelectQuyen();
            if (dt == null)
            {
                aq = new Entities.Quyen[0];
                return;
            }
            else
            {
                aq = new Entities.Quyen[dt.Rows.Count];
                int i = 0;
                foreach (DataRow item in dt.Rows)
                {
                    Entities.Quyen q = new Entities.Quyen();
                    q.QuyenID = int.Parse(item[0].ToString());
                    q.TenForm = item[1].ToString();
                    q.Ten = (item[2].ToString());

                    aq[i++] = q;
                }
            }
            cbbQuyen.DataSource = aq;
            cbbQuyen.DisplayMember = "Ten";
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {
            Entities.ChiTietQuyen ctq = new Entities.ChiTietQuyen();
            ctq.TenNhomQuyen = tennhomquyen;
            ctq.TenForm = ((Entities.Quyen)cbbQuyen.SelectedItem).TenForm;
            ctq.Ten = ((Entities.Quyen)cbbQuyen.SelectedItem).Ten;
            bool c = true;
            foreach (Entities.ChiTietQuyen item in actq)
            {
                if (item.TenForm == ctq.TenForm)
                {
                    c = false;
                    break;
                }
            }

            if (c)
            {
                int i = Common.StaticVariable.InterfaceObject.InsertChiTietQuyen(ctq);
                if (i != 1)
                {
                    MessageBox.Show("Thêm quyền lỗi");
                }
                else
                {

                    ThemCTQ(ctq);
                    MessageBox.Show("Thêm thành công");
                }
            }
            
            
        }
    }
}
